#include <avr/io.h>
#include <avr/interrupt.h>

#include "main.h"
#include "mail.h"
#include "serial.h"
#include "disp.h"
#include "rtc.h"
#include "key.h"
#include "manage.h"

// Peripheral initialization
#define TICKRATE 1000	// time 1 ms

#define TCNT_INIT (0xFF-((F_CPU/256)/TICKRATE))

ISR(TIMER0_OVF_vect)
{
	TimeMail();

	TimeSerial();
	
	TimeDisp();
	
	TimeRTC();
	
	TimeKeyIn();
		
	TimeManage();
	
	TCNT0 = TCNT_INIT;
}

int main(void)
{

	MCUCR = _BV(SE);
	TCNT0 = TCNT_INIT;
	TCCR0 =  (1<<CS02);         //clk/256 (From prescaler)
	TIMSK = _BV(TOIE0);
    
	InitMail();

	InitSerial();
	
	InitDisp();
	
	InitRTC();
	
	InitKeyIn();
	
	InitManage();
	
	sei();

	while(TRUE)
	{	
		MailTask();

		SerialTask();
		
		DispTask();
		
		RTCTask();
		
		KeyInTask();
		
		ManageTask();
	}
}

